//
//  UserFollowCell.m
//  danpin
//
//  Created by chuangjia on 5/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "UserFollowCell.h"

@interface UserFollowCell ()
{
    
}
@property(nonatomic,strong)UIImageView *imagesView;
@property(nonatomic,strong)RTLabel *titleLabel;
@property(nonatomic,strong)UIButton *followBtn;
@property(nonatomic,strong)UIImageView *lineView;

@end
@implementation UserFollowCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imagesView.layer.cornerRadius=PHOTO_FRAME_WIDTH*2;
        _imagesView.layer.masksToBounds = YES;
        [self.contentView addSubview:_imagesView];
        _titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING)*2,0)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = UIColorFromRGB(0x808080);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = FONT(14);
        _titleLabel.lineSpacing =LSpacing;
        [self.contentView addSubview:_titleLabel];
        
        _followBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _followBtn.backgroundColor =UIColorFromRGB(0x41c339);
        _followBtn.titleLabel.font =FONT(13);
        _followBtn.layer.cornerRadius=3;
        [_followBtn addTarget:self action:@selector(settingbtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_followBtn];
        
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.image =[UIImage imageNamed:@"line_xu.png"];
        [self.contentView addSubview:_lineView];
        
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.imagesView.image=nil;
    self.imagesView=nil;
    self.titleLabel=nil;
    self.lineView =nil;
//     DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    self.lineView.frame =CGRectMake(0,self.bounds.size.height-1, DEVICE_WIDTH, 1);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    _imagesView.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING, PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
    NSString * strURL =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"largeFace"]] replaceNullString];
    [_imagesView sd_setImageWithURL:[NSURL URLWithString:strURL] placeholderImage:[UIImage imageNamed:@"user_face.png"]];
    NSString *test =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"nick_name"]] replaceNullString];
    [_titleLabel setText:test];
    CGSize optimumSize = [_titleLabel optimumSize];
    
    _titleLabel.frame =CGRectMake(_imagesView.frame.origin.x+_imagesView.frame.size.width+PHOTO_FRAME_WIDTH,(PHOTO_FRAME_WIDTH*6+PHOTO_TIME_PADDING*2-optimumSize.height)/2,optimumSize.width, optimumSize.height);
    _titleLabel.text =test;
    
    
    if([key isEqualToString:@"mefollow"])
    {
        _followBtn.frame=CGRectMake( DEVICE_WIDTH- widthAuto(70)-PHOTO_FRAME_WIDTH,(PHOTO_FRAME_WIDTH*6+PHOTO_TIME_PADDING*2-hightAuto(30))/2, widthAuto(70), hightAuto(30));
        int follow =[[dict objectForKey:@"followed"] intValue];
        if (follow==2) {
            _followBtn.backgroundColor =UIColorFromRGB(0xe8e8e8);
            [_followBtn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
            [_followBtn setTitle:@"互相关注" forState:UIControlStateNormal];
        }else if (follow==1) {
            _followBtn.backgroundColor =UIColorFromRGB(0xe8e8e8);
            [_followBtn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
            [_followBtn setTitle:@"取消关注" forState:UIControlStateNormal];
        }
        
    }else if([key isEqualToString:@"fans"])
    {
        _followBtn.frame=CGRectMake( DEVICE_WIDTH- widthAuto(70)-PHOTO_FRAME_WIDTH,(PHOTO_FRAME_WIDTH*6+PHOTO_TIME_PADDING*2-hightAuto(30))/2, widthAuto(70), hightAuto(30));
        int follow =[[dict objectForKey:@"followed"] intValue];
        if (follow==2) {
            _followBtn.backgroundColor =UIColorFromRGB(0xe8e8e8);
            [_followBtn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
            [_followBtn setTitle:@"互相关注" forState:UIControlStateNormal];
        }else if (follow==1) {
            _followBtn.backgroundColor =UIColorFromRGB(0xe8e8e8);
            [_followBtn setTitleColor:UIColorFromRGB(0x666666) forState:UIControlStateNormal];
            [_followBtn setTitle:@"取消关注" forState:UIControlStateNormal];
        }else if (follow==0) {
            _followBtn.backgroundColor =UIColorFromRGB(0x41c339);
            _followBtn.titleLabel.font =FONT(13);
            [_followBtn setTitle:@"关注" forState:UIControlStateNormal];
        }
        
    }else if([key isEqualToString:@"followRecommed"])
    {
        _followBtn.frame=CGRectMake( DEVICE_WIDTH- widthAuto(70)-PHOTO_FRAME_WIDTH,(PHOTO_FRAME_WIDTH*6+PHOTO_TIME_PADDING*2-hightAuto(30))/2, widthAuto(70), hightAuto(30));
        _followBtn.backgroundColor =UIColorFromRGB(0x41c339);
        _followBtn.titleLabel.font =FONT(13);
         [_followBtn setTitle:@"关注" forState:UIControlStateNormal];
    }
    
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)settingbtnClick
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_userfollow,self,self.data,nil,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
//    NSDictionary *dict =(NSDictionary *)model;
   
    return PHOTO_FRAME_WIDTH*6+PHOTO_TIME_PADDING*2;
}
@end
